﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Model;
using Hangjing.SQLServerDAL;

public partial class Admin_AdminList : AdminPageBase
{
    EAdmin dal = new EAdmin();

    IList<RoleInfo> role_list = new List<RoleInfo>();

    Role rbll = new Role();

    protected string SqlWhere
    {
        set
        {
            ViewState["sqlwhere"] = value;
        }
        get
        {
            return ViewState["sqlwhere"] == null ? "" : ViewState["sqlwhere"].ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //角色
            WebUtility.BindList("RoleID", "R_RoleName", new sys_Roles().getall("1=1"), ddlRem);
            WebUtility.BindList("cid", "cname", SectionProxyData.GetCityList(), DDLArea);

            SqlWhere = new RightsTool().getAdminWhere(SqlWhere);

            GetData();
        }
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        SqlWhere = "1=1";
        SqlWhere = new RightsTool().getAdminWhere(SqlWhere);
        if (this.tbAdminName.Text != "")
        {
            SqlWhere += " and  AdminName like '%" + WebUtility.InputText(this.tbAdminName.Text) + "%'";
        }
        if (this.tbRealName.Text != "")
        {
            SqlWhere += " and  RealName like '%" + WebUtility.InputText(this.tbRealName.Text) + "%'";
        }
        if (DDLArea.SelectedValue != "-1")
        {
            SqlWhere += " and cityid = " + DDLArea.SelectedValue;
        }

        if (ddlPermission.SelectedValue != "-1")
        {
            SqlWhere += " and Permission=" + ddlPermission.SelectedValue;
        }
        if (ddlRem.SelectedValue != "-2")
        {
            SqlWhere += " and Rem=" + ddlRem.SelectedValue;
        }
        if (tbgroup.Text != "")
        {
            SqlWhere = SqlWhere + " and  EXISTS(SELECT * FROM dbo.DeliverGroup WHERE ID = dbo.EAdmin.AdminStatus AND classname  LIKE '%" + WebUtility.InputText(tbgroup.Text) + "%')";
        }


        GetData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        GetData();
    }

    protected string Permistion(object inpara)
    {
        int n = Convert.ToInt32(inpara);

        string ret = "普通管理员";
        switch (Convert.ToInt32(inpara))
        {
            case 0:
                ret = "普通管理员"; break;
            case 1:
                ret = "超級管理员"; break;
            case 10:
                ret = "市场部专员"; break;
        }

        return ret;
    }

    protected void rptUserList_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {

        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "del":
                {
                    //判断权限
                    int _rs = WebUtility.checkOperator(4);
                    if (_rs == 0)
                    {
                        AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
                        return;
                    }
                    if (dal.Delete(id) > 0)
                    {
                        AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown('提示信息','text:刪除成功!','250','150','true','1000','true','text');init();");
                        GetData();
                    }
                    else
                    {
                        AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown('提示信息','text:刪除失敗!','250','150','true','1000','true','text');init();");
                        GetData();
                    }
                    SectionProxyData.ClearMarketAdminList();
                }
                break;
        }
    }

    protected void DelList_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator(4);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }
        string IdList = this.hdDels.Value;
        if (dal.Delete(IdList) > 0)
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown('提示信息','text:删除成功!','250','150','true','1000','true','text');init();");
            GetData();
        }
        else
        {
            AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown('提示信息','text:删除失败!','250','150','true','1000','true','text');init();");
            GetData();
        }
        SectionProxyData.ClearMarketAdminList();
    }

    protected void GetData()
    {
        this.AspNetPager1.RecordCount = dal.GetCount(SqlWhere);
        IList<EAdminInfo> list = dal.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere);
        IList<CityInfo> citys = CacheHelper.GetCityList();
        IList<DeliverGroupInfo> groups = SectionProxyData.GetEdelivergroupList();
        foreach (var item in list)
        {
            item.cityname = "不限";
            item.Groupname = "不限";
            foreach (var city in citys)
            {
                if (city.cid == item.CityID)
                {
                    item.cityname = city.cname;

                }
            }

            foreach (var group in groups)
            {
                if (group.ID ==  item.AdminStatus)
                {
                    item.Groupname = group.classname;
                }
            }
        }

        this.rtpUserlist.DataSource = list;
        this.rtpUserlist.DataBind();

        AlertScript.RegScript(this.Page, this.UpdatePanel1, "init();");
    }

}
